package com.guldan.core.utils;

/**
 * Created by Silas.
 * Date: 2016/10/1
 * Time: 22:23
 */
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public class SystemUtil {
    private static final int BYTE_ARRAY_SIZE = 8192;

    public SystemUtil() {
    }

    public static <T> Class<T> getClassGenricType(Class clazz) {
        return getClassGenricType(clazz, 0);
    }

    public static Class getClassGenricType(Class clazz, int index) {
        Type genType = clazz.getGenericSuperclass();
        if(!(genType instanceof ParameterizedType)) {
            return Object.class;
        } else {
            Type[] params = ((ParameterizedType)genType).getActualTypeArguments();
            return index < params.length && index >= 0?(!(params[index] instanceof Class)?Object.class:(Class)params[index]):Object.class;
        }
    }

    public static <T> List<T> deepCopy(List<T> src) {
        try {
            ByteArrayOutputStream e = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(e);
            out.writeObject(src);
            ByteArrayInputStream byteIn = new ByteArrayInputStream(e.toByteArray());
            ObjectInputStream in = new ObjectInputStream(byteIn);
            List dest = (List)in.readObject();
            return dest;
        } catch (Exception var6) {
            var6.printStackTrace();
            return null;
        }
    }

    public static Object deepCopy(Object src) {
        try {
            ByteArrayOutputStream e = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(e);
            out.writeObject(src);
            ByteArrayInputStream byteIn = new ByteArrayInputStream(e.toByteArray());
            ObjectInputStream in = new ObjectInputStream(byteIn);
            Object dest = in.readObject();
            return dest;
        } catch (Exception var6) {
            var6.printStackTrace();
            return null;
        }
    }

    public static byte[] fromObjectToBinary(Object o) {
        try {
            ByteArrayOutputStream ex = new ByteArrayOutputStream(8192);
            ObjectOutputStream oos = new ObjectOutputStream(ex);

            try {
                oos.writeObject(o);
            } finally {
                oos.close();
            }

            return ex.toByteArray();
        } catch (IOException var7) {
            var7.printStackTrace();
            return null;
        }
    }

    public static Object fromBinaryToObject(byte[] binary) {
        try {
            ByteArrayInputStream ex = new ByteArrayInputStream(binary);
            ObjectInputStream ois = new ObjectInputStream(ex);

            Object var3;
            try {
                var3 = ois.readObject();
            } finally {
                ois.close();
            }

            return var3;
        } catch (Exception var8) {
            var8.printStackTrace();
            return null;
        }
    }
}
